package CutLoad.transitions.distortion
{
	import CutLoad.Base;
	import CutLoad.cookies.LaterRun;
	import CutLoad.cookies.Painter;
	
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;

	/**
	 * 图片扭曲梯形过渡 
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class DistortImageTrapezoidalTransition extends Base
	{
		private var ptl:Point,ptr:Point,pdr:Point,pdl:Point;
		//最初的源点
		private var init_ptl:Point,init_ptr:Point,init_pdr:Point,init_pdl:Point;
		private var start_ptl:Point,start_ptr:Point,start_pdr:Point,start_pdl:Point;
		private var DI:DistortImage;
		private var target:DisplayObject,bitmapdata:BitmapData;
		//
		private var laterRun:LaterRun;
		/**
		 * 初始化
		 * @param target 需要模仿的目标
		 * @param delayPhotoFrames 延时生成目标的图形，因为有时候太快了，获的目标还没有完成就生成图片了。默认为5
		 * 
		 */		
		public function DistortImageTrapezoidalTransition(target:DisplayObject,delayPhotoFrames:Number=24)
		{
			this.target=target;
			target.visible=false;
			//
			new LaterRun(delayPhotoFrames,startInit);
		}
		private function startInit():void{
			bitmapdata=new BitmapData(target.width,target.height,true,0xffffff);
			bitmapdata.draw(target);
			//
			DI=new DistortImage(target.width,target.height);
			//
			ptl=new Point();
			ptr=new Point(target.width,0);
			pdr=new Point(target.width,target.height);
			pdl=new Point(0,target.height);
			//
			init_ptl=ptl.clone();
			init_ptr=ptr.clone();
			init_pdr=pdr.clone();
			init_pdl=pdl.clone();
			//
			ptl.x+=target.width*0.3+20;
			ptl.y+=target.height*0.1+20;
			ptr.x-=target.width*0.3+20
			ptr.y+=target.height*.1+20;
			//
			pdr.x-=target.width*.2;
			pdr.y-=target.height*.3-10;
			pdl.x+=target.width*.2
			pdl.y-=target.height*.3;
			//
			start_ptl=ptl.clone();
			start_ptr=ptr.clone()
			start_pdr=pdr.clone()
			start_pdl=pdl.clone()
			//
			//
			this.addEventListener(Event.ENTER_FRAME,enterFrame);
			visible=false;
			//
		}
		public function stop():void{
			visible=false;
		}
		private function enterFrame(e:Event):void{
			if(!visible) return;
			this.graphics.clear();
			DI.setTransform(graphics,bitmapdata,ptl,ptr,pdr,pdl);
		}
		public function getOpTarget():DisplayObject{
			return target;
		}
		public function easeIn():void{
			ptl=start_ptl.clone();
			ptr=start_ptr.clone();
			pdr=start_pdr.clone();
			pdl=start_pdl.clone();
			//
			visible=true;
			target.visible=false;
			if(laterRun!=null) laterRun.stop();
			//
			///*
			TweenLite.to(ptl,0.3, {x:init_ptl.x, y:init_ptl.y,ease:Circ.easeOut});
			TweenLite.to(ptr,0.4, {x:init_ptr.x, y:init_ptr.y,ease:Circ.easeOut});
			TweenLite.to(pdr,0.5, {x:init_pdr.x, y:init_pdr.y,ease:Circ.easeInOut});
			TweenLite.to(pdl,0.5, {x:init_pdl.x, y:init_pdl.y,ease:Circ.easeInOut,onComplete:easeIn_onComplete});
			//
			this.alpha=0;
			TweenLite.to(this, 0.2, {alpha:1,ease:Circ.easeInOut});
			//*/
		}
		public function easeOut():void{
			/*ptl=init_ptl.clone();
			ptr=init_ptr.clone();
			pdr=init_pdr.clone();
			pdl=init_pdl.clone();*/
			//
			//
			visible=true;
			target.visible=false;
			if(laterRun!=null) laterRun.stop();
			TweenLite.to(ptl,0.5, {x:start_ptl.x, y:start_ptl.y,ease:Circ.easeInOut});
			TweenLite.to(ptr,0.5, {x:start_ptr.x, y:start_ptr.y,ease:Circ.easeInOut});
			TweenLite.to(pdr,0.8, {x:start_pdr.x, y:start_pdr.y,ease:Circ.easeInOut});
			TweenLite.to(pdl,0.8, {x:start_pdl.x, y:start_pdl.y,ease:Circ.easeInOut});
			TweenLite.to(this, 0.5, {alpha:0,ease:Circ.easeInOut});
		}
		private function easeIn_onComplete():void{
			//visible=false;
			if(laterRun!=null) laterRun.stop();
			target.visible=true;
			visible=false;
			return;
			laterRun=new LaterRun(target.stage.frameRate,function():void{
				target.visible=true;
				visible=false;
			});
		}
		private function easeOut_onComplete():void{
			//trace("easeOut_onComplete");
			//visible=false;
		}
	}
}